package com.bw.home

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.bw.home.entity.LoginEntity
import com.bw.home.intent.HomeIntent
import com.bw.home.viewmodel.HomeViewModel
import com.bw.lib_common.NavPath
import com.bw.lib_miv.IUiState
import com.bw.lib_miv.RequestType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


@Composable
fun UpdatePasswordPage(
    navHostController: NavHostController,
    vm: HomeViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {
    var username by remember {
        mutableStateOf("")
    }

    var password by remember {
        mutableStateOf("")
    }

    var newPassword by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {

                    is IUiState.OnSuccess -> {
                        if (it.type == RequestType.LOGIN) {
                            val entity = it.data as LoginEntity
                            vm.sendIntent(HomeIntent.updatePassword(entity.id, newPassword))
                        } else if (it.type == RequestType.UPDATE_PASSWORD) {
                            ToastUtils.showLong("修改成功")
                            navHostController.popBackStack()
                        }
                    }

                    is IUiState.OnFailed -> {
                        Log.d("TagA", "UpdatePasswordPage: ${it.msg}")
                    }

                    else -> {}
                }
            }
        }
    })

    Column(
        Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .padding(5.dp)
    ) {
        Box(
            Modifier
                .fillMaxWidth()
                .height(70.dp)

        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .fillMaxHeight(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Default.KeyboardArrowLeft,
                    contentDescription = "",
                    modifier = Modifier.clickable {
                        navHostController.popBackStack()
                    })
                Text(text = "修改密码", Modifier.weight(1f), textAlign = TextAlign.Center)
            }
        }

        Text(text = "账号")
        OutlinedTextField(
            value = username,
            onValueChange = { username = it },
            modifier = Modifier
                .fillMaxWidth()
                .height(60.dp)
        )

        Spacer(modifier = Modifier.height(20.dp))

        Text(text = "密码")
        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            modifier = Modifier
                .fillMaxWidth()
                .height(60.dp)
        )


        Spacer(modifier = Modifier.height(20.dp))

        Text(text = "输入要修改的新密码")
        OutlinedTextField(
            value = newPassword,
            onValueChange = { newPassword = it },
            modifier = Modifier
                .fillMaxWidth()
                .height(60.dp)
        )


        Spacer(modifier = Modifier.height(20.dp))


        Button(onClick = {
            vm.sendIntent(HomeIntent.login(username, password))
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "确认修改")
        }

    }
}